package com.example.happiness.controller.user;

import com.example.happiness.pojo.Serve;
import com.example.happiness.result.Result;
import com.example.happiness.service.user.UserServeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "服务项接口")
@Slf4j
@RestController
@RequestMapping("/user/serve")
public class UserServeController {

    @Autowired
    UserServeService userServeService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("servelist")
    @ApiOperation("获取服务列表")
    public Result<List<Serve>> getServeList(Integer cid){
        log.info("获取类型id：{}",cid);
        String key = "serve_"+cid;
        List<Serve> list = (List<Serve>) redisTemplate.opsForValue().get(key);
        if(list != null && list.size()>0){
            return Result.success(list);
        }
        list = userServeService.getServeList(cid);
        redisTemplate.opsForValue().set(key,list);

        return Result.success(list);
    }

    @GetMapping("/serve")
    @ApiOperation("获取服务详情")
    public Result<Serve> getserve(Integer sid){
        Serve serve = userServeService.getserve(sid);
        return Result.success(serve);
    }

    @GetMapping("/getFoundationServe")
    @ApiOperation("获取基本服务")
    public Result getFoundationServe(String sname){
        Integer sid = userServeService.getBysname(sname);
        return Result.success(sid);
    }

    @GetMapping("/getHotServe")
    @ApiOperation("获取热门服务")
    public Result<List<Serve>> getHotServe(){
        List<Serve> list = userServeService.getHotServe();
        return Result.success(list);
    }
}
